﻿using Kingdee.BOS;
using Kingdee.BOS.App.Data;
using Kingdee.BOS.Contracts.Report;
using Kingdee.BOS.Core.Enums;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceHelper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace PMAF.K3Cloud.SCM.Report.ServicePlugIn
{
    [Kingdee.BOS.Util.HotUpdate]
    [Description("采销多空单明细报表插件")]
    public class PurSalOrderReportService : SysReportBaseService
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
            //简单账表类型：普通、树形、分页
            ReportProperty.ReportType = ReportType.REPORTTYPE_NORMAL;
            //报表显示顺序字段
            //ReportProperty.IdentityFieldName = "FIDENTITYID";
            //是用插件创建临时表
            IsCreateTempTableByPlugin = true;
            //是否分组汇总
            ReportProperty.IsGroupSummary = true;
        }

        /// <summary>
        /// 过滤条件填充至表头
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public override ReportTitles GetReportTitles(IRptParams filter)
        {
            base.GetReportTitles(filter);
            ReportTitles reportTitles = new ReportTitles();
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            if(customFilter != null)
            {
                string supplierNames = GetSupplierNames((DynamicObjectCollection)customFilter["F_PMAF_Supplier"]);
                reportTitles.AddTitle("F_PMAF_Supplier", supplierNames);
                DynamicObject factory = (DynamicObject)customFilter["F_PMAF_Factory"];
                string factoryName = factory == null ? "" : factory["Name"].ToString();
                reportTitles.AddTitle("F_PMAF_MatGroup", factoryName);
                string result = customFilter["F_PMAF_Result"].ToString();
                string resultStr = result == "" ? "" : Convert.ToInt32(result) == 0 ? "多空数量为正" : "多空数量为负";
                reportTitles.AddTitle("F_PMAF_Result", resultStr);
                var startDate = customFilter["F_PMAF_StartDate"];
                var endDate = customFilter["F_PMAF_EndDate"];
                string dateZone = GetDateZone(startDate, endDate);
                reportTitles.AddTitle("F_PMAF_DateZone", dateZone);
                string orgIds = customFilter["F_PMAF_BusinessOrgs"].ToString();
                string orgNames = GetOrgNames(orgIds);
                reportTitles.AddTitle("F_PMAF_BusinessOrgs", orgNames);
                string contractTypes = GetContractTypeTitle(customFilter["F_PMAF_ContractType"].ToString());
                reportTitles.AddTitle("F_PMAF_ContractType", contractTypes);
            }
            return reportTitles;
        }

        /// <summary>
        /// 设置报表列
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            ReportHeader header = new ReportHeader();
            header.AddChild("PURCHASEQTY", new LocaleValue("采购数量", Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("INSTOCKQTY", new LocaleValue("采购装车数量", Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("PURAVAILBABLEQTY", new LocaleValue("采购可提数量", Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("SALEQTY", new LocaleValue("销售数量", Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("REMAINQTY", new LocaleValue("多空数量", Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("MATERIALNUMBER", new LocaleValue("物料编码", Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            header.AddChild("FMATERIALNAME", new LocaleValue("物料名称", Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            header.AddChild("SUPPLIERNAME", new LocaleValue("供应商", Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            header.AddChild("PURCHASESN", new LocaleValue("采购流水号", Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            header.AddChild("FCONTRACTTYPE", new LocaleValue("合同类型", Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            header.AddChild("FACTORYNAME", new LocaleValue("油厂", Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            header.AddChild("PORGNAME", new LocaleValue("业务组织", Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            //header.AddChild("SORGNAME", new LocaleValue("销售组织", Context.UserLocale.LCID), SqlStorageType.Sqlvarchar);
            //header.AddChild("FTAXPRICE", new LocaleValue("采购单价", Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            //header.AddChild("STOCKOUTQTY", new LocaleValue("销售装车数量", Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            //header.AddChild("SALAVAILBABLEQTY", new LocaleValue("销售可提数量", Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            header.AddChild("COST", new LocaleValue("现货成本", Context.UserLocale.LCID), SqlStorageType.SqlDecimal);
            return header;
        }

        /// <summary>
        /// SQL语句构建报表数据 F_PMAF_Factory_Id F_PMAF_PurchaseIds F_PMAF_SaleIds F_PMAF_Result ""
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="tableName"></param>
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            base.BuilderReportSqlAndTempTable(filter, tableName);
            DynamicObject customFilter = filter.FilterParameter.CustomFilter;
            int factoryId = Convert.ToInt32(customFilter["F_PMAF_Factory_Id"]);
            string factoryStr = factoryId == 0 ? "" : string.Format(" AND t4.FID IN ({0}) ", Convert.ToString(factoryId));
            //DynamicObjectCollection purchases = (DynamicObjectCollection)customFilter["F_PMAF_PurchaseIds"];
            //string purchasesStr = GetPurchases(purchases);
            //DynamicObjectCollection salers = (DynamicObjectCollection)customFilter["F_PMAF_SaleIds"];
            //string salersStr = GetSales(salers);
            string businessOrgIds = customFilter["F_PMAF_BusinessOrgs"].ToString();
            string businessOrgStr = GetBusinessOrgs(businessOrgIds);
            string result = customFilter["F_PMAF_Result"].ToString();
            string resultStr = GetResult(result);
            //string startDate = customFilter["F_PMAF_StartDate"].ToString();
            string startDateStr = customFilter["F_PMAF_StartDate"] == null ? "" : string.Format(" AND t8.FDELIVERYDATE>='{0}'", customFilter["F_PMAF_StartDate"]);
            //string endDate = customFilter["F_PMAF_EndDate"].ToString();
            string endDateStr = GetEndDate(customFilter["F_PMAF_EndDate"]);
            DynamicObjectCollection suppliers = (DynamicObjectCollection)customFilter["F_PMAF_Supplier"];
            string supplierStr = GetSupIdFilterStr(suppliers);
            string contractTypeStr = GetContractType(customFilter["F_PMAF_ContractType"].ToString());
            string sqlStr = string.Format(@"/*dialect*/
            SELECT
            t1.F_PMAF_STOPSTATUS_H AS STOPSTATUS,
            t1.FCUSTID,
            t1.FSALEORGID,
            t0.FMATERIALID,
            t0.F_PMAF_PURCHASESN AS PURCHASESN,
            t0.F_PMAF_CONTRACTTYPE AS CONTRACTTYPE,
            t0.FQTY,
            t2.FSTOCKBASEOUTJOINQTY
            INTO #TEMP0
            FROM T_SAL_ORDERENTRY t0
            LEFT JOIN T_SAL_ORDER t1 ON t1.FID=t0.FID
            LEFT JOIN T_SAL_ORDERENTRY_R t2 ON t2.FID=t0.FID AND t2.FENTRYID=t0.FENTRYID
            LEFT JOIN T_SAL_ORDERENTRY_D t8 ON t8.FID=t0.FID AND t8.FENTRYID=t0.FENTRYID
            WHERE 1=1{3}{4};
            UPDATE #TEMP0 SET FQTY=FSTOCKBASEOUTJOINQTY WHERE STOPSTATUS='B';
            SELECT
            e0.PFACTORYNAME,
            e0.FSUPPLIERID,
            e0.SUPPLIERNAME,
            e0.FCLOSESTATUS,
            e0.PORGNAME,
            e0.PMATERIALNUMBER,
            e0.PMATERIALNAME,
            e0.PPURCHASESN,
            e0.PURCHASEQTY,
            e0.INSTOCKQTY,
            (e0.PURCHASEQTY-e0.INSTOCKQTY) AS PURAVAILBABLEQTY,
            e0.FTAXPRICE,
            e0.PCONTRACTTYPE,
            e1.SFACTORYNAME,
            e1.SORGNAME,
            e1.SMATERIALNUMBER,
            e1.SMATERIALNAME,
            e1.SPURCHASESN,
            e1.SCONTRACTTYPE,
            e1.SALEQTY,
            e1.STOCKOUTQTY
            INTO #TEMP
            FROM 
            (SELECT
            t1.FSUPPLIERID,
            t1.FCLOSESTATUS,
            t7.FNAME AS SUPPLIERNAME,
            t4.FNAME AS PFACTORYNAME,
            t5.FNAME AS PORGNAME,
            t2.FNUMBER AS PMATERIALNUMBER,
            t3.FNAME AS PMATERIALNAME,
            t0.F_PMAF_PURCHASESN AS PPURCHASESN,
            t0.F_PMAF_CONTRACTTYPE AS PCONTRACTTYPE,
            t0.FQTY AS PURCHASEQTY,
            t9.INSTOCKQTY,
            t6.FTAXPRICE
            FROM T_PUR_POORDERENTRY t0
            LEFT JOIN T_PUR_POORDER t1 ON t1.FID=t0.FID
            LEFT JOIN T_BD_MATERIAL t2 ON t2.FMATERIALID=t0.FMATERIALID
            LEFT JOIN T_BD_MATERIAL_L t3 ON t3.FMATERIALID=t0.FMATERIALID
            LEFT JOIN T_BD_MATERIALGROUP_L t4 ON t4.FID=t2.FMATERIALGROUP
            LEFT JOIN T_ORG_ORGANIZATIONS_L t5 ON t5.FORGID=t1.FPURCHASEORGID
            LEFT JOIN T_PUR_POORDERENTRY_F t6 ON t6.FENTRYID=t0.FENTRYID AND t6.FID=t0.FID
            LEFT JOIN T_BD_SUPPLIER_L t7 ON t7.FSUPPLIERID=t1.FSUPPLIERID
            LEFT JOIN T_PUR_POORDERENTRY_D t8 ON t8.FID=t0.FID AND t8.FENTRYID=t0.FENTRYID
            LEFT JOIN
            (SELECT
            t0.F_PMAF_PURCHASESN,
            CASE
	            WHEN t2.FREALQTY IS NULL THEN 0
	            ELSE t2.FREALQTY
            END AS INSTOCKQTY
            FROM T_PUR_POORDERENTRY t0
            LEFT JOIN
            (SELECT
            FPOORDERENTRYID,
            SUM(FREALQTY) AS FREALQTY
            FROM T_STK_INSTOCKENTRY
            GROUP BY FPOORDERENTRYID) t2 ON t2.FPOORDERENTRYID=t0.FENTRYID) t9 ON t0.F_PMAF_PURCHASESN=t9.F_PMAF_PURCHASESN
            WHERE t3.FLOCALEID=2052
            AND t4.FLOCALEID=2052
            AND t4.FNAME<>'期货'
            AND t5.FLOCALEID=2052
            AND t7.FLOCALEID=2052
            AND t0.F_PMAF_ISCOLLECT_E=1
            {1}{3}{4}
            ) e0 FULL OUTER JOIN (
            SELECT
            t4.FNAME AS SFACTORYNAME,
            t5.FNAME AS SORGNAME,
            t1.FNUMBER AS SMATERIALNUMBER,
            t2.FNAME AS SMATERIALNAME,
            t6.FNUMBER AS SPURCHASESN,
            t0.CONTRACTTYPE AS SCONTRACTTYPE,
            SUM(t0.FQTY) AS SALEQTY,
            SUM(t0.FSTOCKBASEOUTJOINQTY) AS STOCKOUTQTY
            FROM #TEMP0 t0
            LEFT JOIN T_BD_MATERIAL t1 ON t1.FMATERIALID=t0.FMATERIALID
            LEFT JOIN T_BD_MATERIAL_L t2 ON t2.FMATERIALID=t0.FMATERIALID
            LEFT JOIN T_BD_MATERIALGROUP_L t4 ON t4.FID=t1.FMATERIALGROUP
            LEFT JOIN T_ORG_ORGANIZATIONS_L t5 ON t5.FORGID=t0.FSALEORGID
            LEFT JOIN PMAF_t_PurchaseSn t6 ON t6.FID=t0.PURCHASESN
            WHERE t0.FQTY<>0
            AND t2.FLOCALEID=2052
            AND t4.FLOCALEID=2052
            AND t4.FNAME<>'期货'
            AND t5.FLOCALEID=2052{1}
            GROUP BY t0.CONTRACTTYPE,t1.FNUMBER,t2.FNAME,t4.FNAME,t5.FNAME,t6.FNUMBER
            ) e1 ON e0.PMATERIALNUMBER=e1.SMATERIALNUMBER
            AND e0.PFACTORYNAME=e1.SFACTORYNAME
            AND e0.PPURCHASESN=e1.SPURCHASESN
            AND e0.PCONTRACTTYPE=e1.SCONTRACTTYPE;
            UPDATE #TEMP SET PMATERIALNUMBER=SMATERIALNUMBER WHERE PORGNAME IS NULL;
            UPDATE #TEMP SET PMATERIALNAME=SMATERIALNAME WHERE PORGNAME IS NULL;
            UPDATE #TEMP SET PFACTORYNAME=SFACTORYNAME WHERE PORGNAME IS NULL;
            UPDATE #TEMP SET PURCHASEQTY=0 WHERE PORGNAME IS NULL;
            UPDATE #TEMP SET PPURCHASESN=SPURCHASESN WHERE PORGNAME IS NULL;
            UPDATE #TEMP SET PORGNAME=SORGNAME WHERE PORGNAME IS NULL;
            UPDATE #TEMP SET SALEQTY=0 WHERE SORGNAME IS NULL;
            UPDATE #TEMP SET STOCKOUTQTY=0 WHERE SORGNAME IS NULL;
            UPDATE #TEMP SET FTAXPRICE=0 WHERE PORGNAME IS NULL;
            UPDATE #TEMP SET PURAVAILBABLEQTY=0 WHERE FCLOSESTATUS='B' AND PURAVAILBABLEQTY<>0;
            UPDATE #TEMP SET PCONTRACTTYPE=SCONTRACTTYPE WHERE PCONTRACTTYPE IS NULL;
            SELECT
            ROW_NUMBER() OVER(ORDER BY PMATERIALNUMBER) AS FIDENTITYID,
            PMATERIALNUMBER AS MATERIALNUMBER,
            FSUPPLIERID,
            SUPPLIERNAME,
            PORGNAME,
            SORGNAME,
            PMATERIALNAME AS FMATERIALNAME,
            CASE
	            WHEN PCONTRACTTYPE=1 THEN '基差合同'
	            WHEN PCONTRACTTYPE=2 THEN '点价合同'
	            WHEN PCONTRACTTYPE=3 THEN '现货合同'
	            WHEN PCONTRACTTYPE IS NULL THEN '空单'
            END AS FCONTRACTTYPE,
            PPURCHASESN AS PURCHASESN,
            PFACTORYNAME AS FACTORYNAME,
            PURCHASEQTY,
            INSTOCKQTY,
            PURAVAILBABLEQTY,
            FTAXPRICE AS COST,
            SALEQTY,
            STOCKOUTQTY,
            CASE
	            WHEN PURCHASEQTY-STOCKOUTQTY<0 THEN 0
	            ELSE PURCHASEQTY-STOCKOUTQTY
            END AS SALAVAILBABLEQTY,
            PURCHASEQTY-SALEQTY AS REMAINQTY
            INTO {0}
            FROM #TEMP
            WHERE 1=1{2}{5}{6}{7};
            DROP TABLE #TEMP0;
            DROP TABLE #TEMP;", tableName, factoryStr, resultStr, startDateStr, endDateStr, supplierStr, businessOrgStr, contractTypeStr);
            DBUtils.ExecuteDynamicObject(Context, sqlStr);
        }

        /// <summary>
        /// 设置汇总列 
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public override List<SummaryField> GetSummaryColumnInfo(IRptParams filter)
        {
            var result = base.GetSummaryColumnInfo(filter);
            result.Add(new SummaryField("PURCHASEQTY", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("SALEQTY", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("REMAINQTY", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("INSTOCKQTY", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("PURAVAILBABLEQTY", BOSEnums.Enu_SummaryType.SUM));
            result.Add(new SummaryField("STOCKOUTQTY", BOSEnums.Enu_SummaryType.SUM));
            return result;
        }

        /// <summary>
        /// 获取组织过滤条件
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        private string GetBusinessOrgs(string ids)
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(ids))
            {
                string[] idsStr = ids.Split(',');
                sb.Append(" AND PORGNAME IN (");
                int i = 0;
                foreach (string idStr in idsStr)
                {
                    //设置组织过滤条件并查询组织
                    OQLFilter orgFilter = new OQLFilter
                    {
                        new OQLFilterHeadEntityItem() { FilterString = string.Format("FORGID = '{0}'", idStr) }
                    };
                    DynamicObject[] orgs = BusinessDataServiceHelper.Load(Context, "ORG_Organizations", null, orgFilter);
                    string orgName = orgs[0]["NAME"].ToString();
                    sb.Append("'").Append(orgName).Append("'");
                    if(i < idsStr.Length - 1)
                    {
                        sb.Append(",");
                    }
                    i++;
                }
                sb.Append(")");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 生成采购员过滤字符串
        /// </summary>
        /// <param name="purchases"></param>
        /// <returns></returns>
        //private string GetPurchases(DynamicObjectCollection purchases)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    if(purchases.Count > 0)
        //    {
        //        sb.Append(" AND PURCHASENUMBER IN (");
        //        int i = 0;
        //        foreach ( var item in purchases)
        //        {
        //            DynamicObject purchase = (DynamicObject)item["F_PMAF_PurchaseIds"];
        //            sb.Append("'");
        //            sb.Append(Convert.ToString(purchase["NUMBER"]));
        //            sb.Append("'");
        //            if(i < purchases.Count - 1)
        //            {
        //                sb.Append(",");
        //            }
        //            i++;
        //        }
        //        sb.Append(")");
        //    }
        //    return sb.ToString();
        //}

        /// <summary>
        /// 生成销售员过滤字符串
        /// </summary>
        /// <param name="sales"></param>
        /// <returns></returns>
        //private string GetSales(DynamicObjectCollection sales)
        //{
        //    StringBuilder sb = new StringBuilder();
        //    if (sales.Count > 0)
        //    {
        //        sb.Append(" AND SALERNUMBER IN (");
        //        int i = 0;
        //        foreach (var item in sales)
        //        {
        //            DynamicObject sale = (DynamicObject)item["F_PMAF_SaleIds"];
        //            sb.Append("'");
        //            sb.Append(Convert.ToString(sale["NUMBER"]));
        //            sb.Append("'");
        //            if (i < sales.Count - 1)
        //            {
        //                sb.Append(",");
        //            }
        //            i++;
        //        }
        //        sb.Append(")");
        //    }
        //    return sb.ToString();
        //}

        /// <summary>
        /// 生成供应商过滤字符串
        /// </summary>
        /// <param name="sales"></param>
        /// <returns></returns>
        private string GetSupIdFilterStr(DynamicObjectCollection suppliers)
        {
            StringBuilder sb = new StringBuilder();
            if (suppliers.Count > 0)
            {
                sb.Append(" AND FSUPPLIERID IN (");
                int i = 0;
                foreach (var item in suppliers)
                {
                    DynamicObject supplier = (DynamicObject)item["F_PMAF_Supplier"];
                    sb.Append(supplier["Id"]);
                    if (i < suppliers.Count - 1)
                    {
                        sb.Append(",");
                    }
                    i++;
                }
                sb.Append(")");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 生成多空结果字符串
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        private string GetResult(string result)
        {
            StringBuilder sb = new StringBuilder();
            if(result != null && result != "")
            {
                sb.Append(" AND PURCHASEQTY-SALEQTY");
                if (string.Equals(result, "0"))
                {
                    sb.Append(">0");
                }
                else
                {
                    sb.Append("<0");
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 结束时间字符串
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        private string GetEndDate(object date)
        {
            StringBuilder sb = new StringBuilder();
            if (date != null)
            {
                string time = "00:00:00";
                string rTime = "23:59:59";
                string dateStr = date.ToString().Replace(time, rTime);
                sb.Append(string.Format(" AND t8.FDELIVERYDATE<='{0}'", dateStr));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 合同类型过滤条件
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private string GetContractType(string str)
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(str))
            {
                sb.Append(" AND PCONTRACTTYPE IN (");
                string[] contractTypes = str.Split(',');
                int i = 0;
                foreach(string contractType in contractTypes)
                {
                    sb.Append(contractType);
                    if (i < contractTypes.Length - 1)
                    {
                        sb.Append(',');
                    }
                    i++;
                }
                sb.Append(")");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 取得供应商的值
        /// </summary>
        /// <param name="suppliers"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private string GetSupplierNames(DynamicObjectCollection suppliers)
        {
            StringBuilder sb = new StringBuilder();
            if (suppliers.Count > 0)
            {
                int i = 0;
                foreach (DynamicObject item in suppliers)
                {
                    DynamicObject supplier = (DynamicObject)item["F_PMAF_Supplier"];
                    sb.Append(supplier["Name"]);
                    if (i < suppliers.Count - 1)
                    {
                        sb.Append(";");
                    }
                    i++;
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 取得交货日期范围
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <returns></returns>
        private string GetDateZone(object startDate, object endDate)
        {
            StringBuilder sb = new StringBuilder();
            if (startDate != null)
            {
                sb.Append(startDate.ToString().Substring(0, 10));
            }
            else
            {
                sb.Append("--");
            }
            if(endDate != null)
            {
                sb.Append("至").Append(endDate.ToString().Substring(0, 10));
            }
            else
            {
                sb.Append("至--");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 取得组织名称字段
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        private string GetOrgNames(string ids)
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(ids))
            {
                string[] idsStr = ids.Split(',');
                int i = 0;
                foreach (string idStr in idsStr)
                {
                    //设置组织过滤条件并查询组织
                    OQLFilter orgFilter = new OQLFilter
                    {
                        new OQLFilterHeadEntityItem() { FilterString = string.Format("FORGID = '{0}'", idStr) }
                    };
                    DynamicObject[] orgs = BusinessDataServiceHelper.Load(Context, "ORG_Organizations", null, orgFilter);
                    string orgName = orgs[0]["NAME"].ToString();
                    sb.Append(orgName);
                    if (i < idsStr.Length - 1)
                    {
                        sb.Append("; ");
                    }
                    i++;
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 取得表头合同类型
        /// </summary>
        /// <returns></returns>
        private string GetContractTypeTitle(string contractType)
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(contractType))
            {
                string[] contractTypes = contractType.Split(',');
                int i = 0;
                foreach(string contractTypeStr in contractTypes)
                {
                    if (contractTypeStr == "1")
                    {
                        sb.Append("基差合同");
                    }
                    else if(contractTypeStr == "2")
                    {
                        sb.Append("点价合同");
                    }
                    else
                    {
                        sb.Append("现货合同");
                    }
                    if (i < contractTypes.Length - 1)
                    {
                        sb.Append(';');
                    }
                    i++;
                }
            }
            return sb.ToString();
        }
    }
}
